package com.lyj.linearalgebra.user.service;

import com.lyj.linearalgebra.user.dao.UserDao;
import com.lyj.linearalgebra.user.domain.User;

import cn.itcast.commons.CommonUtils;

public class UserService {
	private UserDao userDao = new UserDao();

	public void regist(User form) throws UserException{
		
		//校验用户
		User user = userDao.findByUserName(form.getUsername());
		if(user != null) {
			throw new UserException("用户名已被注册!");
		}
		user = userDao.findByEmail(form.getEmail());
		if(user != null) {
			throw new UserException("Email已被注册!");
		}
		
		userDao.add(form);
		
	}

	public User login(User form) throws UserException {
		
		User user = userDao.findByUserName(form.getUsername());
		if(user == null) {
			throw new UserException("用户名不存在!");
		}else if(!user.getPassword().equals(form.getPassword())) {
			throw new UserException("密码错误!");
		}
		return user;
	}
	
	public String getCode(String email) throws UserException {
		User user = userDao.findByEmail(email);
		if(user != null) {
			throw new UserException("Email已被注册!");
		}
		return CommonUtils.uuid() + CommonUtils.uuid();
	}

}
